package cn.enjoy.sharding.controller;

import cn.enjoy.sharding.entity.Course;
import cn.enjoy.sharding.mapper.CourseMapper;
import cn.enjoy.tool.api.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author K
 * @date 2022/1/14 17:30
 */
@RequestMapping("course")
@RestController
public class CourseController {
    @Resource
    private CourseMapper courseMapper;


    @ApiOperation("新增 水平分表分库")
    @PostMapping("insert")
    public R<Integer> insert(@RequestBody Course course) {
        int insert = courseMapper.insert(course);
        return R.data(insert);
    }

    @ApiOperation("查询 水平分表")
    @GetMapping("getById")
    public R<Course> getById(Long id) {
        return R.data(courseMapper.selectById(id));
    }

    @ApiOperation("查询 水平分表分库")
    @GetMapping("getByIdAndUId")
    public R<Course> getByIdAndUId(Long id, Long uid) {
        Course course = courseMapper.selectOne(Wrappers.<Course>lambdaQuery()
                .eq(Course::getId, id)
                .eq(Course::getUserId, uid));
        return R.data(course);
    }

    @ApiOperation("查询 获取1条数据 水平分库分表")
    @GetMapping("getLimit")
    public R<Course> getLimit(Long id) {
        return R.data(courseMapper.getLimit(id));
    }

}
